﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjetSMA.Model
{
    public class Decision
    {
        private List<Action> m_action;
        private List<Action> m_action_true = new List<Action>();
        

        public Decision()
        {
            m_action = new List<Action>();
            m_action.Add(new Action("_hunting" ));
            m_action.Add(new Action("_breeding"));
            m_action.Add(new Action("_eating"  ));
            m_action.Add(new Action("_escaping"));
        }

        public void UpdateList(string par_name, int par_value, bool par_state)
        {
            foreach (Action loc_action in m_action)
            {
                if (loc_action.Name == par_name)
                {
                    if(loc_action.Value<10)
                        loc_action.Value += par_value;
                    loc_action.State = par_state;
                }
            }
        }
        
        public void ResetValue(string par_name, int par_value)
        {
            foreach (Action loc_action in m_action)
            {
                if (loc_action.Name == par_name)           
                    loc_action.Value = par_value;                
            }
        }

        public void ResetState()
        {
            foreach (Action loc_action in m_action)
            {
                loc_action.State = false;
            }
        }
        
        public string ChoseDecision()
        {
            Random loc_rd = new Random();
            double loc_proba = loc_rd.NextDouble();
            FillActionTrue();
            m_action_true.Sort(Compare);            

            //Console.WriteLine("Random: " + loc_proba);
            if (m_action_true.Count == 1)
            {
                double loc_test = 1 - (m_action_true[0].Value*0.1);
               // Console.WriteLine("Ope: " + loc_test + "Name: " + m_action_true[0].Name);
                if (loc_proba > 1 - (m_action_true[0].Value *0.1))
                {
                    
                    return m_action_true[0].Name;
            }
            }
            else if (m_action_true.Count == 2)
            {
                int loc_total = m_action_true[0].Value + m_action_true[1].Value;

                //Decimal.Divide(,);

                if (loc_proba > 1 - (double)(m_action_true[1].Value / loc_total) - 0.03)
                    return m_action_true[1].Name;
                else if (loc_proba <= 0.03)
                    return "_nothing";
                else
                    return m_action_true[0].Name;              
            }
            else if (m_action_true.Count == 3)
            {
                int loc_total = m_action_true[0].Value + m_action_true[1].Value + m_action_true[2].Value;

                if (loc_proba > 1 - (double)(m_action_true[2].Value / loc_total) - 0.01)
                    return m_action_true[2].Name;                
                else if (loc_proba <= 0.01)                
                    return "_nothing";                
                else if (loc_proba > 0.01 && loc_proba <= (double)(m_action_true[2].Value / loc_total))                
                    return m_action_true[0].Name;                
                else                
                    return m_action_true[1].Name;                
            }
            return "_nothing";
        }

        public int Compare(object x, object y)
        {
            Action N1 = (Action)x;
            Action N2 = (Action)y;

            if (N1.Value < N2.Value)
                return -1;
            else if (N1.Value > N2.Value)
                return 1;
            else return 0;
        }

        private void FillActionTrue()
        {
            m_action_true = new List<Action>();
            foreach (Action loc_action in m_action)
            {
                if (loc_action.Value > 10)
                    loc_action.Value = 10;
                if (loc_action.Value < 0)
                    loc_action.Value = 0;
                if (loc_action.State == true)
                    m_action_true.Add(loc_action);
            }
        }
     
        public int FindValue(string par_name)
        {
            foreach (Action loc_action in m_action)
            {
                if (loc_action.Name == par_name)
                    return loc_action.Value;
            }
            return 0;
        }

        public List<Action> ListAction
        {
            get { return m_action; }
        }

       

        



    }
}
